package com.online.controller;

import com.alibaba.fastjson.JSONObject;
import com.online.entity.*;
import com.online.service.StudentAnswerService;
import com.online.service.StudentService;
import com.online.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping(value = "/student")
public class StudentController {

    @Autowired
    StudentService studentService;

    @Autowired
    UserService userService;

    @Autowired
    StudentAnswerService studentAnswerService;


    /**
     * 教师插入一个学生的信息
     * @param studentNumber
     * @param studentName
     * @param studenClass
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addStudent", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    public String addStudent(@RequestParam(value = "studentNumber",required = false) String studentNumber,
                             @RequestParam(value = "studentName",required = false) String studentName,
                             @RequestParam(value = "studentClass",required = false) String studenClass) {

//        查询该学生信息是否存在
        Student stu = studentService.showByStudentNumber(studentNumber);

        if(stu != null) {
            ErrorJsonObject errorJsonObject = new ErrorJsonObject(102,"添加重复");
            return JSONObject.toJSONString(errorJsonObject);
        }

        Student student = new Student();
        student.setStudentNumber(studentNumber);
        student.setStudentName(studentName);
        student.setStudentClass(studenClass);
//        设置初始密码为账号
        student.setPassword(studentNumber);
        student.setStatus(0);

        int result = studentService.addARecord(student);

        User user = new User();
        user.setUserNumber(studentNumber);
        user.setPassword(studentNumber);
        user.setRole("student");
        user.setStatus(0);

        result = userService.addARecord(user);

        if(result != 0) {
            CorrectJsonObject correctJsonObject = new CorrectJsonObject("操作成功");
            return JSONObject.toJSONString(correctJsonObject);
        }else {
            ErrorJsonObject errorJsonObject = new ErrorJsonObject(101,"插入失败");
            return JSONObject.toJSONString(errorJsonObject);
        }
    }


    /**
     * 向数据库批量导入学生数据
     *
     * @param httpSession
     * @param httpServletRequest
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/importStudent", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    public String importStudent(HttpSession httpSession, HttpServletRequest httpServletRequest) {

        //获取教师选择的班级
        String studentClass = (String) httpSession.getAttribute("studentClass");
        httpSession.removeAttribute("studentClass");

        //初始化变量
        Student student = new Student();
        User user = new User();
        int result = 0;

        //向数据库导入数据
        List<Student> studentList = (List<Student>) httpSession.getAttribute("studentList");
        for (int i = 0; i < studentList.size(); i++) {

            student.setStudentName(studentList.get(i).getStudentName());
            student.setPassword(studentList.get(i).getPassword());
            student.setStudentNumber(studentList.get(i).getStudentNumber());
            student.setStatus(0);
            student.setStudentClass(studentClass);

            user.setUserNumber(studentList.get(i).getStudentNumber());
            user.setPassword(studentList.get(i).getPassword());
            user.setRole("student");
            user.setStatus(0);
            result = studentService.addARecord(student);
            if (result == 1) {
                result = userService.addARecord(user);
            }
        }

        if (result == 1) {
            /*httpSession.removeAttribute("studentList");
            httpServletRequest.setAttribute("result", "导入数据成功!");
            return "teacher/count_student";*/
            httpSession.removeAttribute("studentList");
            CorrectJsonObject correctJsonObject = new CorrectJsonObject("插入成功");
            return JSONObject.toJSONString(correctJsonObject);
        } else {
            ErrorJsonObject errorJsonObject = new ErrorJsonObject(113,"插入失败");
            return JSONObject.toJSONString(errorJsonObject);
        }
    }


    /**
     * 查询一个班级的学生列表
     *
     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = "/showStudentList", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    public String showStudentList(HttpServletRequest httpServletRequest) {
        //获取教师选择的班级
        String studentClass = httpServletRequest.getParameter("studentClass");
        //更加班级查询一个班级里学生的具体信息
        List<Student> studentList = studentService.showByStudentClass(studentClass);
        if (studentList.size() != 0) {
            httpServletRequest.setAttribute("studentList", studentList);
            return "teacher/student_list";
        } else {
            httpServletRequest.setAttribute("result", "暂无添加任何学生");
            return "teacher/student_list";
        }
    }


    /**
     * 教师删除一个学生
     * @param studentId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteStudentById", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    public String deleteAStudent(@RequestParam(value = "studentId") Integer studentId) {

        //根据id获取学生的基本信息
        Student student = studentService.showStudentById(studentId);

        //从student表删除一条学生记录
        int result = studentService.deleteStudentById(studentId);

        //从user表中删除该用户数据
        result = userService.deleteAUserByUserNumber(student.getStudentNumber());

        //查询此用户的答案
        List<StudentAnswer> list = studentAnswerService.showStudentAnswerList(student.getStudentNumber());

        //删除此用户的所有答案
        if(!list.isEmpty()) {
            result = studentAnswerService.deleteRecordByStudentNumber(student.getStudentNumber());
        }

        if (result != 0) {
            CorrectJsonObject correctJsonObject = new CorrectJsonObject("删除成功");
            return JSONObject.toJSONString(correctJsonObject);
        } else {
            ErrorJsonObject errorJsonObject = new ErrorJsonObject(101,"删除失败");
            return JSONObject.toJSONString(errorJsonObject);
        }
    }

    /**
     * 教师禁用学生
     * @param studentId
     * @param status
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/forbidden", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    public String forbiddenStudent(@RequestParam(value = "studentId",required = false) Integer studentId,
                                   @RequestParam(value = "status",required = false) Integer status) {
//        获取学生的信息
        Student student = studentService.showStudentById(studentId);

//        更新学生表的权限
        int result = studentService.updateStatus(studentId,status);
//        更新用户表的登录
        result = userService.updateUserStatus(student.getStudentNumber(),status);

        if(result != 0) {
            CorrectJsonObject correctJsonObject = new CorrectJsonObject("更新成功");
            return JSONObject.toJSONString(correctJsonObject);
        }else {
            ErrorJsonObject errorJsonObject = new ErrorJsonObject(101,"更新失败");
            return JSONObject.toJSONString(errorJsonObject);
        }
    }


    /**
     * 获取当前登录学生的基本信息返回登录学生的个人中心
     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = "/getStudentInfo",method = RequestMethod.GET,produces = "text/html;charset=UTF-8")
    public String showStudentInfo(HttpServletRequest httpServletRequest) {

        //获取当前登录用户
        Subject subject = SecurityUtils.getSubject();

        //获取当前登录学生的学号
        String studentNumber = (String) subject.getPrincipal();

        Student student = studentService.showByStudentNumber(studentNumber);

        if(student != null) {
            httpServletRequest.setAttribute("student",student);
            return "student/student_info";
        }else {
            httpServletRequest.setAttribute("result","暂无你的信息");
            return "student/student_info";
        }
    }


    /**
     * 更新当前学生登录的基本信息
     * @param studentName
     * @param sex
     * @param studentId
     * @param password
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateStudentInfo",method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
    public String updateInfo(@RequestParam(value = "studentName",required = false) String studentName,
                             @RequestParam(value = "sex",required = false) String sex,
                             @RequestParam(value = "studentId",required = false) Integer studentId,
                             @RequestParam(value = "password",required = false) String password) {

        //获取当前用户
        Subject subject = SecurityUtils.getSubject();
        //获取userNumber
        String userNumber = (String) subject.getPrincipal();

        //更新student的基本信息
        int result = studentService.updateStudentInfo(studentId,studentName,password,sex);

        //更新登录信息
        result = userService.updateUserByUserNumber(userNumber,password);

        if(result != 0) {
            CorrectJsonObject correctJsonObject = new CorrectJsonObject("更新成功");
            return JSONObject.toJSONString(correctJsonObject);
        }else {
            ErrorJsonObject errorJsonObject = new ErrorJsonObject(106,"更新失败");
            return JSONObject.toJSONString(errorJsonObject);
        }
    }
}

